<?php
namespace app\zmt\controller;
use app\zmt\model\BannerModel;
use cmf\controller\AdminBaseController;


class BannerController extends AdminBaseController
{

    //列表页面显示
    public function index()
    {

        $list = BannerModel::order(['list_order'=>'DESC','id'=>'DESC'])
            ->paginate(5);
        $this->assign('list',$list);
        return $this->fetch('banner');
    }

    //添加页面显示
    public function add()
    {
       return $this->fetch();
    }

    //添加
    public function addPost()
    {

        //图片获取器
        $image = $_POST['image'];
        $image = cmf_get_image_url($image);

        //添加
        $add = BannerModel::create([
            'image' => $image,
            'url' => '#'
        ]);
        
        if($add){
            $this->success('添加成功','banner/index');
        }else{
            $this->error('添加失败');
        }
    }

    //编辑页面显示
    public function edit()
    {

        //接收id
        $id = input('id/d','');

        //根据id查找内容
        $list = BannerModel::find($id);
        $this->assign('list',$list);
        return $this->fetch();
    }

    //编辑
    public function editPost()
    {

        //接收编辑数据
        $data = input('post.');

        //图片获取器
        $data['image'] = cmf_get_image_url($data['image']);

        //查询数据是否存在
        $edit = BannerModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到数据');
        }

        //更新
        $edit->image = $data['image'];
        $edit->url = $data['url'];
        if($edit->save()){
            $this->success('保存成功','banner/index');
        }else{
            $this->error('保存失败');
        }

    }

    //删除
    public function delete()
    {
        $id = input('id/d','');

        if(BannerModel::destroy($id)){
            $this->success('删除成功','banner/index');
        }else{
            $this->error('删除失败');
        }
    }

    //排序
    public function px(){
        $nav = new BannerModel();
        parent::listOrders($nav);
        $this->success('排序成功');
    }


}